package com.kumarn.android.imagesave.full.adapter;

import java.util.List;

import com.kumarn.android.imagesave.full.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GoodsListAdapter extends ArrayAdapter<GoodsListItem>{

	private LayoutInflater inflater;
	private List<GoodsListItem> items;
	private int textViewResourceId;


	public GoodsListAdapter(Context context, int textViewResourceId, List<GoodsListItem> items){
		super(context, textViewResourceId,items);

		// リソースIDと表示アイテムを保持
		this.textViewResourceId = textViewResourceId;
    this.items = items;

    // ContextからLayoutInflaterを取得
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 1アイテム分のビューを取得.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View view  = convertView;

		//nullの場合(最初の呼び出し)
		if(view == null){
			view = inflater.inflate(textViewResourceId, null);
		}

		// 対象のアイテムを取得
		GoodsListItem item = items.get(position);

		TextView goodsName = (TextView)view.findViewById(R.id.list_goods_name);
		goodsName.setText(item.getList_goods_name());

		TextView goodsMemo = (TextView)view.findViewById(R.id.list_goods_memo);
		goodsMemo.setText(item.getList_goods_memo());

		TextView goodsGroup = (TextView)view.findViewById(R.id.list_goods_group);
		goodsGroup.setText(item.getList_goods_group());

		ImageView goodsImage =  (ImageView)view.findViewById(R.id.list_goods_image);
		if(item.getList_goods_image() != null){
			goodsImage.setImageBitmap(item.getList_goods_image());
		}

		return view;
	}

}